﻿@{
    Layout = null;
}
@using CNKI.TPI.Web.Search.Model;
@using CNKI.TPI.Web.Base;
<style>
    .containerTableStyle {
        overflow: auto ! important;
            height: 250px ! important;
    }
</style>
<script>
    $(function () {
        enablesave();
        dealchecked(".f-checkbox", ".f-checkboxdb"); //页面加载时处理选中的复选框 ，数据库操作权限
        dealchecked(".f-field", ".f-checkboxfield");//字段权限
        dealchecked(".navchecklist input[type='checkbox']", "input.dbchecknav");//导航权限

        $(".f-checkbox").each(function () {
            //如果选中的是管理，则把字段全选，导航全选并置灰
            if ($(this).val() == "Manage" && $(this).attr("checked") == "checked") {
                disabledinfo();
            }
            if ($(this).val() == "Manage" && $(this).attr("checked") != "checked") {
                enabledinfo();
            }
        })
    })
    function disabledinfo() {
        $(".f-field").attr("disabled", "disabled");
        $(".f-checkboxfield").attr("disabled", "disabled");
        $(".navchecklist input[type='checkbox']").attr("disabled", "disabled");
        $(".dbchecknav").attr("disabled", "disabled");
        $(".setcode").attr("disabled", "disabled");

        $(".f-checkboxfield").attr("checked", "checked");
        $(".f-field").attr("checked", "checked");

        $(".dbchecknav").attr("checked", "checked");
        $(".navchecklist input[type='checkbox']").each(function () {
            $(this).attr("checked", "checked");
        })

    }
    function enabledinfo() {
        $(".f-field").removeAttr("disabled", "disabled");
        $(".f-checkboxfield").removeAttr("disabled", "disabled");
        $(".navchecklist input[type='checkbox']").removeAttr("disabled", "disabled");
        $(".dbchecknav").removeAttr("disabled", "disabled");
        $(".setcode").removeAttr("disabled", "disabled");
    }
    //字段，操作权限，导航全选
    function dealchecked(checkedsub, checkedparent) {
        var m = 0;
        if ($(checkedsub).length > 0) {
            $(checkedsub).each(function () {
                if ($(this).attr("checked") != "checked") {  //遍历判断是否存在没有选中的记录
                    m = 1;
                }
            })
            if (m == 1) {
                $(checkedparent).removeAttr("checked", "checked");
            }
            else {
                $(checkedparent).attr("checked", "checked");
            }
        }
    }
    $(".f-checkboxdb").click(function () {
        if ($(this).attr("checked") == "checked") {
            //全选
            $(".f-checkbox").each(function () {
                $(this).attr("checked", "checked");
            })
        }
        else {
            //取消全选
            $(".f-checkbox").each(function () {
                $(this).removeAttr("checked", "checked");
            })
        }

    })
    $(".f-checkbox").click(function () {

        //如果选中的是管理，则把字段全选，导航全选并置灰
        if ($(this).val() == "Manage" && $(this).attr("checked") == "checked") {
            disabledinfo();
        }
        if ($(this).val() == "Manage" && $(this).attr("checked") != "checked") {
            enabledinfo();
        }

        //循环判断列表中的checkbox，如果全部选中则全选框呈勾选状态
        if ($(this).attr("checked") != "checked") {
            $(".f-checkboxdb").removeAttr("checked", "checked");
        }
        else {
            dealchecked(".f-checkbox", ".f-checkboxdb");
        }

    })
    $(".f-checkboxfield").click(function () {
        if ($(this).attr("checked") == "checked") {
            //全选
            $(".f-field").each(function () {
                $(this).attr("checked", "checked");
            })
        }
        else {
            //取消全选
            $(".f-field").each(function () {
                $(this).removeAttr("checked", "checked");
            })
        }

    })
    $(".f-field").click(function () {

        //循环判断列表中的checkbox，如果全部选中则全选框呈勾选状态
        if ($(this).attr("checked") != "checked") {
            $(".f-checkboxfield").removeAttr("checked", "checked");
        }
        else {
            dealchecked(".f-field", ".f-checkboxfield");
        }

    })

    //导航代码table全选
    function dbcheckNavAll(e) {
        if ($(e).attr("checked") == "checked") {
            $(".navchecklist input[type='checkbox']").each(function () {
                $(this).attr("checked", "checked");
            })
        }
        else {
            $(".navchecklist input[type='checkbox']").each(function () {
                $(this).removeAttr("checked", "checked");
            })
        }
    }
    //导航table记录选中时更新全选按钮状态
    $(".navchecklist input[type='checkbox']").click(function () {
        //循环判断列表中的checkbox，如果全部选中则全选框呈勾选状态
        if ($(this).attr("checked") != "checked") {
            $(".dbchecknav").removeAttr("checked", "checked");
        }
        else {
            dealchecked(".navchecklist input[type='checkbox']", "input.dbchecknav");//导航权限
        }

    })
    //保存
    function dbAuthSave() {
        disablesave();
        var daycount = $("#daycount").val();
        if (daycount == "") {
            messageAlert("请输入每日下载量！");
            enablesave();
            return;
        }
        var maxcount = $("#maxcount").val();
        if (maxcount == "") {
            messageAlert("请输入最大下载量！");
            enablesave();
            return;
        }
        //数据库权限选中列表
        var jsondbid = [];//
        $(".f-checkbox:checked").each(function () {
            jsondbid.push($(this).val());
        })
        //if (jsondbid == "") {
        //    messageAlert("请选择操作权限！");
        //    enablesave();
        //    return;
        //}
        //字段
        var jsonfield = [];//
        $(".f-field:checked").each(function () {
            jsonfield.push($(this).val());
        })
        //if (jsondbid.indexOf("Manage")==-1 && jsonfield == "") {
        //    messageAlert("请选择字段权限！");
        //    enablesave();
        //    return;
        //}
        //导航
        var navstr = "";
        $('.navtable tbody tr').each(function () {
            if ($(this).find("td").eq(0).children().attr("checked") == "checked") {
                var id = $(this).find("td").eq(1).html();
                var name = $(this).find("td").eq(2).html();
                var code = $(this).find("td").eq(4).html();
                navstr += name + ":" + id;
                if (code != "" && code != "所有分类") {
                    navstr += "(" + code + ")";
                }
                navstr += ";";
            }
        })
        var data = {
            "authdblist": jsondbid.toString(), "fieldlist": jsonfield.toString(),
            "daycount": daycount, "maxcount": maxcount, "dbid": $("#DbID").val(),
            "id": $("#GroupUserID").val(), "flage": $("#Flage").val(), "navstr": navstr
        }
        $.ajax({
            type: "POST",
            url: "@Html.Raw(Url.Action("AuthSave", "Admin_User"))",
            data: data,
            success: function (m) {
                if (m == "True") {
                    messageAlert("操作成功！");
                    back();
                }
                else {
                    messageAlert("操作失败！");
                    enablesave();
                }
            },
            error: function (e) {
                messageAlert("操作失败！");
                enablesave();
            }
        })

    }
    function back() {
        ajaxLoad('initload', "@Html.Raw(Url.Action("DBAuthList", "Admin_User"))?id=" + $("#GroupUserID").val() + "&flage=" + $("#Flage").val())
    }
    //初始化树控件
    var myTree;
    //选中的分类代码显示到对应的table列
    function savenave() {
        var selectid = $('#Thiscateid').val();
        var m = $('input:radio[name="Isvisible"]:checked').val();
        var valuestr = "所有分类";
        if (m == 0) {
            var checkstr = myTree.getAllCheckedBranches();
            if ((checkstr == "" || checkstr == "0") && myTree.getAllFatItems() != "") {
                messageAlert("请先选择分类！");
                return;
            }
            else {
                var str = checkstr.toString().split(',');
                valuestr = str.sort().join(','); //排序
            }
        }
        //更新对应列的数据
        var substr = valuestr;
        if (valuestr.length > 30) {
            substr = valuestr.substring(0, 30) + "...";
        }
        $('.navtable tbody tr').each(function () {
            if ($(this).find("td").eq(1).html() == selectid) {
                $(this).find("td").eq(3).html("<a class=\"atitle\" title='" + valuestr + "'>" + substr + "</a>");
                $(this).find("td").eq(4).html(valuestr);
            }

        })
        closenav();
    }
    //已经设置的勾选
    function checkCode() {
        var code = $("#Thiscatecode").val();
        var arrayNodes = code.split(',');
        for (var i = 0; i < arrayNodes.length; i++) {
            var thiscode = arrayNodes[i];
            if (myTree.hasChildren(thiscode)==0) {
                myTree.setCheck(arrayNodes[i], true);
            }
        }
    }
    //--选中
    //设置分类代码，加载树，弹窗
    function setnavcode(catid) {
        var precode = ""; //原来选中的节点
        $('.navtable tbody tr').each(function () {
            if ($(this).find("td").eq(1).html() == catid) {
                precode = $(this).find("td").eq(4).html();
            }
        })
        $("#Thiscatecode").val(precode);
        if (precode == "" || precode == "所有分类") {
            $('input:radio[name="Isvisible"]:eq(0)').attr("checked", 'checked');
            $('.talkDivBgTree').show();
        }
        else {
            $('input:radio[name="Isvisible"]:eq(1)').attr("checked", 'checked');
            $('.talkDivBgTree').hide();
        }
        $("#Thiscateid").val(catid);
        shownav();
        $("#treeboxbox_tree").empty();
        //加载树
        $.ajax({
            type: "GET",
            url: timestamp("@Html.Raw(Url.Action("GetCategoryInfo", "Admin_User"))"),
            data: { "DbCode": $("#DbCode").val(), "catid": catid, "dbID": $("#DbID").val() },
            beforeSend: function () { $("#loadingcode").show(); $(".talkDivBgLoadTree").show(); },
            success: function (msg) {
                if (msg != "") {
                    myTree = new dhtmlXTreeObject("treeboxbox_tree", "100%", "100%", 0);
                    myTree.setImagePath("../Content/dhtmlxTree/codebase/imgs/dhxtree_skyblue/");
                    myTree.enableThreeStateCheckboxes(true);
                    myTree.enableCheckBoxes(1);
                    myTree.enableSmartXMLParsing(true);
                    myTree.loadXMLString(msg);
                    checkCode();
                }
                else {
                    messageAlert("没有获取到分类数据！");
                }
                $("#loadingcode").hide();
                $(".talkDivBgLoadTree").hide();
            },
            error: function (e) {
                messageAlert("系统异常，请稍后重试");
                $("#loadingcode").hide();
                $(".talkDivBgLoadTree").hide();
            }
        })
    }
    function closenav() {
        $(".talkDivBg").hide();
        $(".nav").hide();
    }
    function shownav() {
        $(".talkDivBg").show();
        $(".nav").show();
    }
    function disablesave() {
        $(".btn-warn-f").attr("disabled", "disabled");
    }
    function enablesave() {
        $(".btn-warn-f").removeAttr("disabled", "disabled");
    }
    function navselect(flage) {
        if (flage == 1) {
            $('.talkDivBgTree').show();
        }
        else {
            $('.talkDivBgTree').hide();
        }
    }
</script>
<style>
    .containerTableStyle {
    position:static ! important;
    }
</style>
<input type="hidden" id="GroupUserID" value="@ViewBag.GroupUserID"/>
<input type="hidden" id="DbID" value="@ViewBag.DBID"/>
<input type="hidden" id="DbCode" value="@ViewBag.DbCode"/>
<input type="hidden" id="Flage" value="@ViewBag.Flage"/>
<input type="hidden" id="Thiscateid" />
<input type="hidden" id="Thiscatecode" />
<div class="wrapR-c">
    <div class="box">
        <h2>数据库：@ViewBag.DbName</h2>
        <h3 class='box-h'>操作权限设置</h3>
        <div class="box-b form-wr">

            <label class="f-label block">
                <input type="checkbox" class="f-checkboxdb">全选</label>

            <div class='clearfix'>
                @{ 
                        
                    IList<SessionHelper.DBRightType> listname = SessionHelper.GetAllDBRight(); //所有权限
                    if (listname != null && listname.Count > 0)
                    {
                        for (int i = 0; i < listname.Count; i++)
                        {
                            if (ViewBag.DBRight != null && ViewBag.DBRight.Contains(listname[i]))
                            {
                                    
                    <label class="f-label col col-6">
                        <input type="checkbox" class="f-checkbox" checked="checked" value="@listname[i]">@SessionHelper.GetDBRightDesc(listname[i])</label>
                            }
                            else
                            {
                    <label class="f-label col col-6">
                        <input type="checkbox" class="f-checkbox" value="@listname[i]">@SessionHelper.GetDBRightDesc(listname[i])</label>
                            }


                        }

                    }
                }
            </div>
        </div>
    </div>

    <div class="box">
        <h3 class='box-h'>下载次数设置</h3>
        <div class="box-b form-h form-h-6em form-wr clearfix">

            <div class="f-gp inline-block col col-2">
                <div class="f-lb">
                    <label class="f-label">每日下载量</label>
                </div>
                <div class="f-ct">
                    @{
                        int count1 = ViewBag.DayCount == null ? 0 : ViewBag.DayCount;
                        int count2 = ViewBag.MaxCount == null ? 0 : ViewBag.MaxCount;

                    }
                    <input type="text" class="f-text span3" value="@count1" id="daycount" onkeyup = "value=value.replace(/[^\d]/g,'')" maxlength="10">
                </div>
            </div>

            <div class="f-gp inline-block col col-2">
                <div class="f-lb">
                    <label class="f-label">最大下载量</label>
                </div>
                <div class="f-ct">
                    <input type="text" class="f-text span3" value="@count2" id="maxcount" onkeyup = "value=value.replace(/[^\d]/g,'')" maxlength="10">
                </div>
            </div>

        </div>
    </div>


    <div class="box">
        <h3 class='box-h'>字段权限设置</h3>
        <div class="box-b form-wr">

            <label class="f-label block">
                <input type="checkbox" class="f-checkboxfield">全选</label>
            <div class='clearfix'>

                @{
                    if (ViewBag.DBFieldList != null)
                    {
                        foreach (SYS_CMS_DATABASE_FIELD item in ViewBag.DBFieldList)
                        {
                            if (!item.Field.Trim().StartsWith("SYS_FLD_"))
                            {
                                string field = "," + ViewBag.FieldList + ",";
                                if (ViewBag.FieldList != null && (ViewBag.FieldList == "" || ViewBag.FieldList == "所有字段" || field.Contains("," + item.Field + ",")))
                                {
                                       
                    <label class="f-label col col-6">
                        <input type="checkbox" class="f-field" checked="checked" value="@item.Field">@item.Field</label>
                                }
                                else
                                {
                                         
                    <label class="f-label col col-6">
                        <input type="checkbox" class="f-field" value="@item.Field">@item.Field</label>
       
                                }
                            }
                        }

                    }
                }

            </div>


        </div>
    </div>
    <div class="box">
        <h3 class='box-h box-h-noborder'>导航权限设置</h3>
        <table width="100%" cellspacing="0" cellpadding="0" border="0" class="table table-grey navtable">
            <thead class='th-wr'>
                <tr class="thead">
                    <th class="tdnum">
                        <input type="checkbox" class="dbchecknav" onclick="dbcheckNavAll(this)">序号</th>
                    <th hidden="hidden" style="display: none">ID</th>
                    <th>导航名称</th>
                    <th>分类代码</th>
                    <th hidden="hidden" style="display: none">Code</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                @{
                    int rowNo = 0;
                }
                @if (ViewBag.CLSInfo != null)
                {
                    foreach (System.Data.DataRow dr in ViewBag.CLSInfo.Rows)
                    {
                        rowNo++;
                        string classValue = "";
                        if (rowNo % 2 == 1)
                        {
                            classValue = "even";
                        }
                        else
                        {
                            classValue = "odd";
                        }
                    <tr class='@classValue'>
                        <td class="navchecklist">
                            @{
                        string checkstr = "";
                        if (ViewBag.NavValue != null)
                        {
                            if (ViewBag.NavValue != "")
                            {
                                if (ViewBag.NavValue.Contains(dr["name"].ToString()))
                                {
                                    checkstr = "checked=checked";
                                }
                            }
                            else
                            {
                                if (ViewBag.DBRight != null && ViewBag.DBRight.Contains(SessionHelper.DBRightType.Manage))
                                {
                                    checkstr = "checked=checked";
                                }
                            }
                        }      
                            }
                            <input name="" @checkstr  type="checkbox" value="@dr["id"]" />@rowNo
                        </td>
                        <td class='tc' hidden="hidden" style="display: none">@dr["id"]</td>
                        <td class='tc'>@dr["name"]</td>
                        <td class="tc">
                            @{ string str = dr["clsCode"].ToString() == "" ? "所有分类" : dr["clsCode"].ToString();
                                <a class="atitle" style="overflow:auto"  title="@str">
                                    @Html.Raw(ColumnAliasHelper.SubContent(str, 30));
                                </a>
                            }
                        </td>
                        <td class="tc" style="display: none" hidden="hidden">@dr["clsCode"]</td>
                        <td class="tc">
                            <button class="setcode"  onclick="setnavcode('@dr["id"]')">设置分类代码</button>
                        </td>
                    </tr>
                    }
                }
            </tbody>
        </table>
    </div>

    <div class="tc mt30" style="padding-bottom: 30px">
        <button class="btn btn-m btn-submit btn-warn-f" onclick="dbAuthSave()">完成设置</button>
        <button class="btn btn-m btn-prim-f" onclick="back() ">返回</button>
    </div>

</div>
<div class="talkDivBg" style="display: none;">
    <!--talkDiv弹出层(不带图片，宽550)-->
    <div class="talkDiv talkDivTr talkW-550 form-h nav" style='margin-top: -278px; display: none'>
        <div class="talkDiv-title">
            <span>选择分类代码</span>
            <a class="icon icon-close" title="关闭" onclick="closenav()" href="javascript:void(0);"><i class="iconfont icon-error3"></i></a>
        </div>
        <div id="loadingcode" style="display: none; margin-left: 200px; margin-top: 150px; position: absolute">
            <img src="~/Images/loading.gif" width="50" />
        </div>
        <div class="talkDivBgLoadTree" style="background-color: #000; opacity: 0.1; z-index: 999; filter: alpha(opacity=45); width: 100%; height: 260px; position: absolute; top: 90px; left: 0; bottom: 90px;">
            @*树加载时遮罩层*@
        </div>
        <div class="talkDiv-body talkDiv-body1">
            <div class="f-gp">
                <div class="f-ct">
                    <label class="f-label">
                        <input type="radio" name="Isvisible" class="f-radio" value="1" checked="checked" onclick="navselect('1')">
                        所有分类代码
                    </label>
                    <label class="f-label">
                        <input type="radio" name="Isvisible" value="0" class="f-radio" onclick="navselect('0')">
                        选择分类代码
                    </label>
                </div>
            </div>
            <div class="f-gp" style="margin-top: 15px">
                <div class="f-ct" style="width: 380px; height: 250px; overflow: auto">
                    <div class="talkDivBgTree" style="background-color: #000; opacity: 0.1; z-index: 999; filter: alpha(opacity=45); width: 100%; height: 260px; position: absolute; top: 90px; left: 0; bottom: 90px;">
                        @*遮罩层，点击所有分类时*@
                    </div>
                    <div id="treeboxbox_tree">
                        @*        <ul id="treeli">
                            </ul>*@
                        <!--透明背景-->

                    </div>

                </div>
            </div>
            <div class="f-gp" style="margin-top: 10px; margin-left: 80px">
                <div class="f-ct">
                    <a href="javascript:void(0);" class="btn btn-m btn-prim-f btn-submit" onclick="savenave()">确定</a>
                    &#12288;<a href="javascript:void(0);" class="btn btn-m btn-submit btn-defa-f" onclick="closenav()">取消</a>
                </div>
            </div>

        </div>
    </div>
</div>
<!-- wrapREnd -->
